package com.yxjx.duoxue.payment;

import java.io.Serializable;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.yxjx.duoxue.BaseActionBarActivity;
import com.yxjx.duoxue.R;
import com.yxjx.duoxue.Yx;
import com.yxjx.duoxue.customview.InnerWebView;
import com.yxjx.duoxue.datamodel.DMProduct;
import com.yxjx.duoxue.my.UserLoginActivity;
import com.yxjx.duoxue.network.NwUtils;
import com.yxjx.duoxue.network.UserLoginResponse;
import com.yxjx.duoxue.utils.UIHelper;
import com.yxjx.duoxue.utils.Utils;

public class ProductInfoActivity extends BaseActionBarActivity {
	
	public static final String HIDE_BUY_BUTTON = "HIDE_BUY_BUTTON";
	private WebView webview;
	private String url;
	private DMProduct mProduct;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		
		if (savedInstanceState != null) {
			Serializable savedObject = savedInstanceState
					.getSerializable(Yx.KEY_PRODUCT_OBJECT);
			if (savedObject != null) {
				getIntent().putExtra(Yx.KEY_PRODUCT_OBJECT, savedObject);
			}
		}
		super.onCreate(savedInstanceState);
		setContentView(R.layout.fragment_product_info);
		customizeActionBarTitle("套餐详情");
		customizeUI();
	}
	
	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		outState.putSerializable(Yx.KEY_PRODUCT_OBJECT, mProduct);
	}
	
	@Override
	protected void onRestoreInstanceState(Bundle savedInstanceState) {
		super.onRestoreInstanceState(savedInstanceState);
		if (getIntent() == null) {
			return;
		} else {
			Serializable savedObject = savedInstanceState.getSerializable(Yx.KEY_PRODUCT_OBJECT);
			if (savedObject != null) {
				getIntent().putExtra(Yx.KEY_PRODUCT_OBJECT, savedObject);
			}
		}
	}

	private void customizeUI() {
		
		if (getIntent() == null || getIntent().getExtras() == null) {
			finish();
			return;
		}
		mProduct = (DMProduct) getIntent().getExtras().getSerializable(Yx.KEY_PRODUCT_OBJECT);
		// 订单详情链接过来时不现实buy button
		boolean hideBuyButton = getIntent().getExtras().getBoolean(HIDE_BUY_BUTTON, false);
		if (hideBuyButton) {
			findViewById(R.id.free_audition).setVisibility(View.GONE);
		}
		
		url = NwUtils.REQ_PRODUCT_H5 + mProduct.getId() + NwUtils.webTail();
		webview = (WebView) findViewById(R.id.product_web);
		webview.getSettings().setAppCacheEnabled(true);
		webview.getSettings().setBuiltInZoomControls(false);
        webview.getSettings().setJavaScriptEnabled(true);
        
        webview.setWebChromeClient(new WebChromeClient() {
			@Override
			public void onProgressChanged(WebView view, int newProgress) {
				// TODO Auto-generated method stub
				super.onProgressChanged(view, newProgress);
				if (newProgress == 100) {
					findViewById(R.id.loading).setVisibility(View.INVISIBLE);
				}
			}
		});
        
        webview.loadUrl(url);
        findViewById(R.id.loading).setVisibility(View.VISIBLE);
	}

	public void buy(View view) {
		if (UserLoginResponse.getInstance(this) != null) {
			startOrderPage();
		} else {
			startActivityForResult(new Intent(this, UserLoginActivity.class),
					Yx.REQ_CODE_LOGIN_FOR_BUY);
		}
	}
	
	@Override
	protected void onActivityResult(int arg0, int arg1, Intent arg2) {
		super.onActivityResult(arg0, arg1, arg2);
		switch (arg0) {
		case Yx.REQ_ORDER_PRODUCT:
			if (arg1 == RESULT_OK) {
				finish();
			}
			break;
		case Yx.REQ_CODE_LOGIN_FOR_BUY:
			if (arg1 == RESULT_OK) {
				startOrderPage();
			}
			break;
		default:
			break;
		}
	}

	private void startOrderPage() {
		Intent intent = getIntent();
		intent.setClass(getApplicationContext(), OrderSubmitActivity.class);
		startActivityForResult(intent, Yx.REQ_ORDER_PRODUCT);
	}
	
	/**
	 * 网络加载失败重新刷新页面
	 * @param view
	 */
	public void toRefresh(View view) {
		webview.loadUrl(url);
		findViewById(R.id.loading).setVisibility(View.VISIBLE);
	}
	
}
